package com.tifa.vote.controller;

import com.tifa.vote.bean.Vote;
import com.tifa.vote.bean.VoteNums;
import com.tifa.vote.service.VoteService;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname VoteController
 * @Description
 * @Date 2022/6/15 12:46
 * @Created by hengyuan
 */
@Controller
public class VoteController {

  @Autowired
  VoteService voteService;

  @RequestMapping("/show_votes")
  public String getVotes(Model model) {
    model.addAttribute("votes", voteService.getVotes());
    return "votes";
  }


  @RequestMapping("/show/{id}")
  public String show_vote(@PathVariable("id") Integer id, ModelMap model) {
    Vote voteById = voteService.getVoteById(id);
    List<VoteNums> voteNums = voteById.getVoteNums();
    List<String> tags = new LinkedList<>();
    List<Integer> nums = new LinkedList<>();
    List<Map<String, Object>> out = new LinkedList<>();
    voteNums.forEach(e -> {
          tags.add(e.getItem());
          nums.add(e.getNums());
          Map<String, Object> temp = new HashMap<>();
          temp.put("value", e.getNums());
          temp.put("name", e.getItem());
          out.add(temp);
        }
    );
    model.put("vote", voteById);
    model.put("tags", tags);
    model.put("nums", nums);
    model.put("out", out);
    return "vote_detail";
  }

  @RequestMapping("/delete_vote/{id}")
  public String deleteVote(@PathVariable("id") Integer id) {
    voteService.deleteVoteById(id);
    return "redirect:/show_votes";
  }

  @RequestMapping("/add_vote")
  public String rerutnVote() {
    return "add_vote";
  }


  @RequestMapping("/add_vote_ajaix")
  public String addVoteByAjaix(@RequestBody Map<String, Object> map) {
    var title = String.valueOf(map.get("title"));
    String description = String.valueOf(map.get("description"));
    ArrayList<String> list = new ArrayList<>();
    var a = map.get("list");
    if (a instanceof ArrayList) {
      list.addAll((ArrayList) a);
    }
    Vote vote = new Vote();
    vote.setTitle(title);
    vote.setDescription(description);
    List<VoteNums> voteItemList = new LinkedList<>();
    for (int i = 0; i < list.size(); i++) {
      VoteNums voteNums = new VoteNums();
      voteNums.setNums(0);
      voteNums.setItem(String.valueOf(list.get(i)));
      voteItemList.add(voteNums);
    }
    vote.setVoteNums(voteItemList);
    voteService.addVote(vote);
    return "redirect:/show_votes";
  }

}
